import React from 'react';
import { Link } from 'umi';
import { Collapse, Row, Col, Card } from 'antd';
import moment from 'moment';
import { getDict } from '@/utils/dataDictUtil';
import { useRequest } from '@/hooks';
import { useFlowData } from '@/pages/WorkFlow/components/CustomComponents/Hire/hooks';
import { Talent, SuggestSalary, OriginSalary } from '@/pages/WorkFlow/components/CustomComponents/Hire/containers';
import { LABOUR_PERIOD_TYPE, PROBATION_PERIOD_TYPE } from '@/pages/Recruitment/dictionary';
import { usePositionAndRank } from '@/pages/Recruitment/Hire/hooks';
import { ApprovalLink } from '../containers/StatusDetailLink';
import { HireCard } from '../containers';
import styles from './index.less';

const { Panel } = Collapse;

const ApprovalCard = ({
    employApply = {},
    recruitTalentEmploy = {},
    employId,
    candidateId,
    recruitStatus,
    employStatusHover
}) => {
    const {
        processInstanceId,
        hiDataId
        // labourPeriodNumber,
        // labourPeriodType,
        // probationMonthSalary,
        // probationPeriodNumber,
        // probationPeriodType,
        // remark,
        // suggestedEntryTime,
        // workType,
        // officialMonthSalary,
        // employUrl
    } = employApply;

    const { talentInfo, applyData, commInfo, recordInfo, hireInfo } = useFlowData(hiDataId);
    const childProps = {
        candidateId,
        employId,
        talentInfo,
        applyData,
        commInfo,
        isEdit: false,
        recordInfo,
        hireInfo
    };
    const {
        organizationName // 需求部门
    } = usePositionAndRank(employId);
    const { data: JOB_TYPE } = useRequest(() => getDict('jobNature'), { initialData: [] });
    const getDictName = (value, dict) => dict.find(item => item.value === value)?.text ?? '';

    const getAuditEmployeeName = () => {
        if (employApply.auditEmployeeName) {
            if (employApply.auditDutyName) {
                return `${employApply.auditEmployeeName}（${employApply.auditDutyName}）`;
            }
            return `${employApply.auditEmployeeName}`;
        }
        return '';
    };

    const getLaunchEmployeeName = () => {
        if (employApply.launchEmployeeName) {
            if (employApply.launchDutyName) {
                return `${employApply.launchEmployeeName}（${employApply.launchDutyName}）`;
            }
            return `${employApply.launchEmployeeName}`;
        }
        return '';
    };

    const successLink = {
        text: '已通过',
        color: '#44c69d',
        background: '#ecfff7',
        link: params => <ApprovalLink params={params} />
    };

    const failInfo = {
        text: '已淘汰',
        color: 'rgba(0,0,0,0.45)',
        background: 'rgba(0, 0, 0, 0.05)',
        link: params => <ApprovalLink params={params} />
    };

    const relaunchParams = {
        // labourPeriodNumber,
        // labourPeriodType,
        // probationMonthSalary,
        // probationPeriodNumber,
        // probationPeriodType,
        // remark,
        // suggestedEntryTime,
        // workType,
        // officialMonthSalary,
        // employUrl,
        employId,
        candidateId
    };

    return (
        <div className={styles.ApprovalCardWrap}>
            <HireCard
                title="录用审批记录"
                linkParams={recruitStatus === 4 || recruitStatus === 5 ? relaunchParams : { processInstanceId }}
                recruitStatus={recruitStatus}
                successFootInfo={recruitStatus > 5 ? successLink : ''}
                failInfo={!employApply.sendTime && recruitStatus === 10 ? failInfo : ''}
                employStatusHover={employStatusHover}
            >
                <div className={styles.infoContent} style={{ paddingTop: 0 }}>
                    <Collapse expandIconPosition="right" bordered={false} defaultActiveKey={['1']}>
                        <Panel header="审批流程记录" key="1">
                            <Row className={styles.infoRow}>
                                <Col span={12}>
                                    <span className={styles.label}>审批人：</span>
                                    <span className={styles.value}>{getAuditEmployeeName()}</span>
                                </Col>
                                <Col span={12}>
                                    <span className={styles.label}>审批时间：</span>
                                    <span className={styles.value}>{`${employApply.auditTime}`}</span>
                                </Col>
                            </Row>
                            <Row className={styles.infoRow}>
                                <Col span={12}>
                                    <span className={styles.label}>发起人：</span>
                                    <span className={styles.value}>{getLaunchEmployeeName()}</span>
                                </Col>
                                <Col span={12}>
                                    <span className={styles.label}>发起时间：</span>
                                    <span className={styles.value}>{`${employApply.applyDate}`}</span>
                                </Col>
                            </Row>
                        </Panel>
                        <Panel header="候选人信息" key="2">
                            <Talent {...childProps} />
                        </Panel>
                        <Panel header="薪资信息" key="3">
                            <Row style={{ padding: '8px 16px' }} gutter={[0, 16]}>
                                <Col>
                                    <Card title="原薪资信息" type="inner">
                                        <OriginSalary {...childProps} />
                                    </Card>
                                </Col>
                                <Col>
                                    <Card title="建议薪资" type="inner">
                                        <SuggestSalary {...childProps} />
                                    </Card>
                                </Col>
                            </Row>
                        </Panel>
                        <Panel header="录用信息" key="4">
                            <Row className={styles.infoRow}>
                                <Col span={12}>
                                    <span className={styles.label}>入职岗位：</span>
                                    <span className={styles.value}>{recruitTalentEmploy.entryDutyName}</span>
                                </Col>
                                <Col span={12}>
                                    <span className={styles.label}>入职部门：</span>
                                    <span className={styles.value}>{organizationName}</span>
                                </Col>
                            </Row>
                            <Row className={styles.infoRow}>
                                <Col span={12}>
                                    <span className={styles.label}>工作性质：</span>
                                    <span className={styles.value}>{getDictName(employApply.workType, JOB_TYPE)}</span>
                                </Col>
                            </Row>
                            <Row className={styles.infoRow}>
                                <Col span={12}>
                                    <span className={styles.label}>建议入职时间：</span>
                                    <span className={styles.value}>
                                        {employApply.suggestedEntryTime &&
                                            moment(employApply.suggestedEntryTime).format('YYYY-MM-DD')}
                                    </span>
                                </Col>
                                <Col span={12}>
                                    <span className={styles.label}>劳动合同年限：</span>
                                    <span className={styles.value}>
                                        {employApply.labourPeriodNumber === -1
                                            ? '无固定期限'
                                            : `${employApply.labourPeriodNumber}${getDictName(
                                                  employApply.labourPeriodType,
                                                  LABOUR_PERIOD_TYPE
                                              )}`}
                                    </span>
                                </Col>
                            </Row>
                            <Row className={styles.infoRow}>
                                <Col span={12}>
                                    <span className={styles.label}>试用期：</span>
                                    <span className={styles.value}>{`${employApply.probationPeriodNumber}${getDictName(
                                        employApply.probationPeriodType,
                                        PROBATION_PERIOD_TYPE
                                    )}`}</span>
                                </Col>
                            </Row>
                            <Row className={styles.infoRow}>
                                <Col span={24}>
                                    <span className={styles.label}>补充说明：</span>
                                    <span className={styles.value}>{`${employApply.remark || ''}`}</span>
                                </Col>
                            </Row>
                            <Row className={styles.infoRow}>
                                <Col span={24}>
                                    <span className={styles.label}>通知时间：</span>
                                    <span className={styles.value}>
                                        录用审批通过后系统自动发送录用通知邮件给录用候选人{' '}
                                        <Link to={`/user/hireConfirm?employId=${employId}&action=detail`}>
                                            预览录用信息
                                        </Link>
                                    </span>
                                </Col>
                            </Row>
                        </Panel>
                    </Collapse>
                </div>
            </HireCard>
        </div>
    );
};

export default ApprovalCard;
